package com.flot.yazb.widget.utils;

import com.google.gson.Gson;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

/**
 * json工具类，用来转换带泛型成员变量的对象
 */
public class JsonUtil {

	/**
	 *
	 * @param json
	 * @param clazz ResponseObject的V类型 ,要是data的数据为空，可直接使用Object.class,或则调用StringUtils.getBean（）
     * @return
     */
	public static ResponseObject fromJson(String json, Class clazz) {
		Gson gson = new Gson();
		ResponseObject o=null;
		try {
			Type objectType = type(ResponseObject.class, clazz);
			 o = gson.fromJson(json, objectType);
		}catch (Exception e){

		}
		return o;
	}

	public static Object fromJson(String json, Class responsename, Class clazz) {
		Gson gson = new Gson();
		Object o=null;
		try {


			Type objectType = type(responsename, clazz);

			o = gson.fromJson(json, objectType);

		}catch (Exception e){
			e.printStackTrace();

		}
		return o;
	}
	private static ParameterizedType type(final Class raw, final Type... args) {
		return new ParameterizedType() {
			public Type getRawType() {
				return raw;
			}

			public Type[] getActualTypeArguments() {
				return args;
			}

			public Type getOwnerType() {
				return null;
			}
		};
	}

	public class ResponseObject <V> {
		private String message;
		private String status;
		private String pageSize;
		private String page;
		private String pagetotal;


		private V data;

		public String getPageSize() {
			return pageSize;
		}

		public void setPageSize(String pageSize) {
			this.pageSize = pageSize;
		}

		public String getPage() {
			return page;
		}

		public void setPage(String page) {
			this.page = page;
		}

		public String getPagetotal() {
			return pagetotal;
		}

		public void setPagetotal(String pagetotal) {
			this.pagetotal = pagetotal;
		}
		public String getMessage() {
			return message;
		}

		public void setMessage(String message) {
			this.message = message;
		}

		public String getStatus() {
			return status;
		}

		public void setStatus(String status) {
			this.status = status;
		}

		public V getData() {
			return data;
		}

		public void setData(V data) {
			this.data = data;
		}

		@Override
		public String toString() {
			return "ResponseData{" +
					"message='" + message + '\'' +
					", status='" + status + '\'' +
					", data=" + data +
					'}';
		}
	}
}